//
//  BSVisitorView.swift
//  mySwiftWeibo
//
//  Created by apple on 16/7/18.
//  Copyright © 2016年 itcast沈庆伟. All rights reserved.
//

import UIKit

//代理协议
protocol BSVisitorViewDelegate:NSObjectProtocol{
    
    //声明代理方法
    func visitorViewRegisterClick()
    func visitorViewLoginClick()
}

//自定义访客视图
class BSVisitorView: UIView {
    
    //MARK: - 代理属性
    weak var delegate:BSVisitorViewDelegate?
    
    //一个view从storyboard或xib,系统认为即要能用代码也要能从storyboard或xib加载控件
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //实现init(frame)
    //子类实现了构造函数,就不会继承父类的构造函数,系统会提示加上 init?(coder aDecoder: NSCoder)
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //self.backgroundColor=UIColor.brownColor()
        self.backgroundColor=UIColor(white: 237 / 255.0, alpha: 1)
        self.setupUI()
    }

    //设置UI界面
    //private只有当前文件可以访问,其他文件无法访问
    private func setupUI(){
        //添加子控件
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(houseView)
        self.addSubview(messageLabel)
        self.addSubview(registerButton)
        self.addSubview(loginButton)
        
        //约束
        //使用纯代码约束需要关闭Autoresizing的影响
        iconView.translatesAutoresizingMaskIntoConstraints=false
        coverView.translatesAutoresizingMaskIntoConstraints=false
        houseView.translatesAutoresizingMaskIntoConstraints=false
        messageLabel.translatesAutoresizingMaskIntoConstraints=false
        registerButton.translatesAutoresizingMaskIntoConstraints=false
        loginButton.translatesAutoresizingMaskIntoConstraints=false
        
        //转轮
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //遮罩
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //房子
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //messageLabel
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 260))
        
        //注册按钮
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //登录按钮
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
    }
    
    ///设置访客图片/公开的方法
    func setVisitorViewImage(imageName:String,message:String){
        //隐藏房子
        houseView.hidden=true
        //coverView.hidden=true
        
        self.sendSubviewToBack(coverView)
        
        //修改转轮图片
        iconView.image=UIImage(named: imageName)
        
        messageLabel.text=message
        
    }
    
    ///转轮核心动画/公开的方法
    /*
    使用核心动画来做转轮的旋转
    keypath有哪些值可以设置,可以查看文档搜索CALayer Animatable Properties, CATransform3D Key Paths
    */
    func setIconViewAnimation(){
        let animation=CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue=2 * M_PI
        animation.duration=20
        animation.repeatCount=MAXFLOAT
        
        animation.removedOnCompletion=false
        
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    
    //MARK: - 懒加载
    ///转轮
    private lazy var iconView:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮罩
    private lazy var coverView:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    ///小房子
    private lazy var houseView:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    ///messageLabel
    private lazy var messageLabel:UILabel={
       let label=UILabel(frame: CGRectZero)
        
        label.textAlignment = NSTextAlignment.Center
        label.font=UIFont.systemFontOfSize(16)
        label.textColor=UIColor.darkGrayColor()
        label.numberOfLines=0
        label.text="关注一些人,看看有什么惊喜"
        
        //让控件size根据内容自己调整
        label.sizeToFit()
        
        return label
    }()
    
    ///注册按钮
    private lazy var registerButton:UIButton = {
        
        let button=UIButton(type: UIButtonType.Custom)
        
        //设置
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font=UIFont.systemFontOfSize(18)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //绑定事件
        button.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    ///登录按钮
    private lazy var loginButton:UIButton={
        let button=UIButton(type: UIButtonType.Custom)
        
        //属性
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.titleLabel?.font=UIFont.systemFontOfSize(18)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //绑定事件
        button.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()

    //MARK: - 按钮绑定事件
    func registerButtonClick(){
        //调用代理方法
        delegate?.visitorViewRegisterClick()
    }
    func loginButtonClick(){
        //调用代理方法
        delegate?.visitorViewLoginClick()
    }
    
}
